﻿using System;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using WeatherHistory.Data.Entities;
using WeatherHistory.Data.Repositories.Interfaces;

namespace WeatherHistory.Data.Repositories;

public class RegionRepository : RepositoryBase<Region>, IRegionRepository
{
    public RegionRepository(ApplicationDbContext context) : base(context)
    {
    }

    public async Task<Region> FirstOrDefaultWithParentAsync(Expression<Func<Region, bool>> match, CancellationToken cancellationToken)
    {
        return await DbSet
            .Include(x => x.ParentRegion)
            .FirstOrDefaultAsync(match, cancellationToken);
    }

    public async Task<Region> FirstOrDefaultWithChildRegionsAsync(Expression<Func<Region, bool>> match, CancellationToken cancellationToken)
    {
        return await DbSet
            .Include(x => x.ChildRegions)
            .FirstOrDefaultAsync(match, cancellationToken);
    }
}